{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import autogluon as ag\n",
    "from autogluon import ObjectDetection as task"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "打开数据集文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root = './'\n",
    "\n",
    "filename = 'workwear'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用自定义数据集路径和类创建一个数据集实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "data_root = os.path.join(root, filename)\n",
    "dataset_train = task.Dataset(data_root, classes=('blue',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "开始训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_limits = 1*60*60 # 5 days\n",
    "epochs = 30\n",
    "detector = task.fit(dataset_train,\n",
    "                    num_trials=2,\n",
    "                    batch_size=8,\n",
    "                    epochs=epochs,\n",
    "                    lr=ag.Categorical(1.25e-4, 1e-4),\n",
    "                    ngpus_per_trial=1,\n",
    "                    time_limits=time_limits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "返回模型在测试集上的效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_test = task.Dataset(data_root, index_file_name='test', classes=('blue',))\n",
    "\n",
    "test_map = detector.evaluate(dataset_test)\n",
    "print(\"mAP on test dataset: {}\".format(test_map[1][1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "detector.model.export(\"models/workwear/myolov3\", epoch=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从测试集中挑一张图片查看结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = 'ch03_20190401000003_75.jpg'\n",
    "image_path = os.path.join(data_root, 'JPEGImages', image)\n",
    "\n",
    "ind, prob, loc = detector.predict(image_path)\n",
    "#prob = detector.predict(image_path)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
